#!/usr/bin/env node

/**
 * 🏆 行业标准基准测试执行器
 * 
 * 使用方法：
 * npm run benchmark:industry
 * 或
 * node --expose-gc test/benchmark/run-industry-benchmark.ts
 */

import { runIndustryStandardBenchmark } from './industry-standard-benchmark';

async function main() {
    console.log('🚀 准备启动行业标准基准测试...\n');
    
    // 环境检查
    console.log('🔍 环境检查:');
    console.log(`   Node.js: ${process.version}`);
    console.log(`   平台: ${process.platform} ${process.arch}`);
    console.log(`   内存: ${(process.memoryUsage().rss / 1024 / 1024 / 1024).toFixed(2)}GB`);
    console.log(`   垃圾回收: ${global.gc ? '✅ 已启用' : '❌ 未启用 (建议使用 --expose-gc)'}`);
    
    const startTime = Date.now();
    
    try {
        const report = await runIndustryStandardBenchmark();
        
        const duration = (Date.now() - startTime) / 1000 / 60;
        
        console.log('\n' + '='.repeat(80));
        console.log('🏆 行业标准基准测试完成!');
        console.log('='.repeat(80));
        console.log(`⏱️  总耗时: ${duration.toFixed(2)} 分钟`);
        console.log(`📊 测试场景: ${report.summary.totalScenarios} 个`);
        console.log(`🏁 Ceno 胜出: ${report.summary.cenoWins} 项`);
        console.log(`🏁 Axios 胜出: ${report.summary.axiosWins} 项`);
        console.log(`🤝 平局: ${report.summary.ties} 项`);
        console.log('');
        console.log(`🎯 综合评分:`);
        console.log(`   Ceno: ${report.summary.overallScore.ceno.toFixed(1)}/100`);
        console.log(`   Axios: ${report.summary.overallScore.axios.toFixed(1)}/100`);
        
        if (report.summary.overallScore.ceno > report.summary.overallScore.axios) {
            console.log(`\n🏆 整体胜出者: Ceno (领先 ${(report.summary.overallScore.ceno - report.summary.overallScore.axios).toFixed(1)} 分)`);
        } else if (report.summary.overallScore.axios > report.summary.overallScore.ceno) {
            console.log(`\n🏆 整体胜出者: Axios (领先 ${(report.summary.overallScore.axios - report.summary.overallScore.ceno).toFixed(1)} 分)`);
        } else {
            console.log(`\n🤝 平局！两个库性能相当`);
        }
        
        if (report.recommendations.length > 0) {
            console.log('\n💡 建议:');
            report.recommendations.forEach(rec => console.log(`   • ${rec}`));
        }
        
        if (report.warnings.length > 0) {
            console.log('\n⚠️  警告:');
            report.warnings.forEach(warning => console.log(`   • ${warning}`));
        }
        
        console.log('\n📁 详细报告文件已生成，请查看当前目录的 JSON 和 Markdown 文件');
        console.log('='.repeat(80));
        
    } catch (error) {
        console.error('\n❌ 基准测试执行失败:', error);
        process.exit(1);
    }
}

// 处理未捕获的错误
process.on('unhandledRejection', (reason, promise) => {
    console.error('❌ 未处理的Promise拒绝:', reason);
    process.exit(1);
});

process.on('uncaughtException', (error) => {
    console.error('❌ 未捕获的异常:', error);
    process.exit(1);
});

// 启动测试
main().catch(console.error);
